package com.start.manager.controller;

import com.start.manager.domain.common.BillResult;
import com.start.manager.domain.vo.BillVO;
import com.start.manager.domain.vo.DetailVO;
import com.start.manager.service.IDetailService;
import com.start.manager.util.PageUtil;
import com.start.manager.util.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author jian.li
 * @date 2019年 12月31日 12:54:43
 */
@RestController
@RequestMapping(value = "/bill")
public class BillController {

    @Autowired
    private IDetailService detailService;

    @PostMapping(value="/findByCompany")
    @ResponseBody
    public ResponseResult findByCompany(@RequestBody BillVO billVO){
        DetailVO detailVO = new DetailVO();
        //取消分页查询
        detailVO.setSize(0);
        if (StringUtils.isNotBlank(billVO.getCompanyId())){
            detailVO.setCompanyId(billVO.getCompanyId());
        }
        if (!Objects.isNull(billVO.getDataRange()) && billVO.getDataRange().length > 0) {
            detailVO.setDataRange(billVO.getDataRange());
        }
        PageUtil list = detailService.findList(detailVO);
        List<DetailVO> listOS = (List<DetailVO>) list.getData();
        List<BillResult> listResult = CaseToResult(listOS);
        return ResponseResult.success(listResult);
    }

    private List<BillResult> CaseToResult(List<DetailVO> listOS) {
        List<BillResult> listResult = new ArrayList<>();
        listOS.stream().forEach(detailVO -> {
            BillResult result = new BillResult();
            result.setName(detailVO.getGoodsName());
            result.setCount(detailVO.getGoodsAmount());
            result.setPrices(detailVO.getGoodsPrice());
            result.setTotal(detailVO.getGoodsPrice().multiply(new BigDecimal(detailVO.getGoodsAmount())));
            listResult.add(result);
        });
        return listResult;
    }

    @PostMapping(value = "/getData")
    @ResponseBody
    public ResponseResult getList(@RequestBody BillVO billVO){
        Map<String,Object> map  =  detailService.findData(billVO);
        return ResponseResult.success(map);
    }
}
